#include <iostream>
#include <Windows.h>
#include <conio.h>
#include "head.h"
using namespace std;

int main()
{

	while (1)
	{
		//运行游戏
		GameStart();
	}
	return 0;
}

//进行整个对局，返回赢家信息
int GameStart()
{
	Init();		//初始化
	int input = 0;		//键盘输入值
	int winner = 0;		//赢家
	while (1)
	{
		
		//清屏
		system("cls");

		//绘制棋盘
		DrawBoard();
		//获取键盘输入
		input=_getch();
		switch (input)
		{
		case 32:		//空格
		{
			if (Put()==true)
			{
				//1.判断平局
				if(NoWin() == 0)
				{
					MessageBox(NULL, "NoWin", "Chess", MB_OK);
					Init();
					DrawBoard();
				}
				if(BanHand(3)|BanHand(4))
				{
					MessageBox(NULL, "WiteWin", "Chess", MB_OK);
					Init();
					DrawBoard();
				}
				winner=Check();
				//2.更换玩家
				if (g_currentGamer == 1)
					g_currentGamer = 2;
				else
				{
					g_currentGamer = 1;
				}
				
				//3.判断输赢
				if (winner == 1)
				{
					MessageBox(NULL,"BlackWin","Chess", MB_OK);
					Init();
					DrawBoard();
				}
				else if(winner==2)
				{
					MessageBox(NULL, "WiteWin", "Chess", MB_OK);
					Init();
					DrawBoard();
				}
			}
			else
				MessageBox(NULL,"ERROR!","Chess",MB_OK);
		}
		case 72:		//向上
			g_cursorY--;
			break;
		case 80:		//向下
			g_cursorY++;
			break;
		case 75:		//向左
			g_cursorX--;
			break;
		case 77:		//向右
			g_cursorX++;
			break;
		default:
			break;
		}
		if (g_cursorY < 0)
			g_cursorY = BOX - 1;
		else if (g_cursorY > BOX - 1)
			g_cursorY = 0;
		if (g_cursorX < 0)
			g_cursorX = BOX - 1;
		else if (g_cursorX > BOX - 1)
			g_cursorX = 0;
	}
	return 0;
}

//初始化
void Init()
{
	//清空二维数组
	memset(g_checkboard, 0, sizeof(g_checkboard));
	g_currentGamer = 1;		//黑子
	//光标位置
	g_cursorX = BOX / 2;		
	g_cursorY = BOX / 2;
	g_count = 0;

}

//绘制棋盘
void DrawBoard()
{
	for (int i = 0; i < BOX; i++)		//行
	{
		for (int j = 0; j < BOX; j++)	//列
		{
			if (j == g_cursorX && i == g_cursorY)//落子位置
				cout<<"P ";
			else if (g_checkboard[j][i] == 1)
				cout<<"x ";
			else if (g_checkboard[j][i] == 2)
				cout<<"o ";
			else if ((i == 0 && j == 0)||(i == 0 && j == BOX - 1)||(i == BOX - 1 && j == 0)||(i == BOX - 1 && j == BOX - 1))//上下左右四个角
				cout<<". ";
			else if (i == 0||i == BOX - 1)//上下边
				cout<<"- ";
			else if (j == 0||j == BOX - 1)//左右边
				cout<<"| ";
			else
				cout<<"+ ";
		}
		cout<<"\n";
	}
}

//布子,成功返回1，失败返回0
bool Put()
{
	//下子成功，并且该下哪颗子
	if (g_checkboard[g_cursorX][g_cursorY] == 0)
	{
		g_checkboard[g_cursorX][g_cursorY] = g_currentGamer;
		return true;
	}
	else
		return false;
}

//判断平局
int NoWin(){
    int Zero = 0;
    for(int i = 0; i < BOX; i++)
        for(int j = 0; j < BOX; j++)
            if(g_checkboard[i][j] == 0)
                Zero++;
    return Zero;
}
 
//判断输赢
int Check()
{
    int Horizontal = 1;             //水平方向
    int Vertical = 1;               //垂直方向
    int PositiveDirection = 1;      //正斜向
    int NegtiveDirection = 1;       //反斜向
 
    //向下
    for (int i = 1; i < 5; i++)
    {
        if (g_cursorY + i < BOX && g_checkboard[g_cursorX][g_cursorY + i] == g_currentGamer)
            Horizontal++;
        else
            break;
    }
    //向上
    for (int i = 1; i < 5; i++)
    {
        if (g_cursorY - i < BOX && g_checkboard[g_cursorX][g_cursorY - i] == g_currentGamer)
            Horizontal++;
        else
            break;
    }
    if (Horizontal >= 5)
        return g_currentGamer;
 
    //向左
    for (int i = 1; i < 5; i++)
    {
        if (g_cursorX + i < BOX && g_checkboard[g_cursorX + i][g_cursorY] == g_currentGamer)
            Vertical++;
        else
            break;
    }
    //向右
    for (int i = 1; i < 5; i++)
    {
        if (g_cursorX - i < BOX && g_checkboard[g_cursorX - i][g_cursorY] == g_currentGamer)
            Vertical++;
        else
            break;
    }
    if (Vertical >= 5)
        return g_currentGamer;
 
    //坐上
    for (int i = 1; i < 5; i++)
    {
        if (g_cursorX - i < BOX && g_cursorY - i < BOX && g_checkboard[g_cursorX - i][g_cursorY - i] == g_currentGamer)
            PositiveDirection++;
        else
            break;
    }
    //右下
    for (int i = 1; i < 5; i++)
    {
        if (g_cursorX + i < BOX && g_cursorY + i < BOX && g_checkboard[g_cursorX + i][g_cursorY + i] == g_currentGamer)
            PositiveDirection++;
        else
            break;
    }
    if (PositiveDirection >= 5)
        return g_currentGamer;
 
    //左下
    for (int i = 1; i < 5; i++)
    {
        if (g_cursorX - i < BOX && g_cursorY + i < BOX && g_checkboard[g_cursorX - i][g_cursorY + i] == g_currentGamer)
            NegtiveDirection++;
        else
            break;
    }
    //右上
    for (int i = 1; i < 5; i++)
    {
        if (g_cursorX + i < BOX && g_cursorY - i < BOX && g_checkboard[g_cursorX + i][g_cursorY - i] == g_currentGamer)
            NegtiveDirection++;
        else
            break;
    }
    if (NegtiveDirection >= 5)
        return g_currentGamer;
 
    return 0;
}
//禁手判断
 
bool BanHand(int N){
    int live[8]={0};
    int threeNum;
    //活子判断
    for(int i = 1;i <= N;i++)
    {//数出每条线上有几个黑子
        if(g_cursorY + i < BOX && g_checkboard[g_cursorX][g_cursorY + i] == 1 && g_checkboard[g_cursorX][g_cursorY + i + 1] != 2 && g_checkboard[g_cursorX][g_cursorY - 1] != 2)//下
            live[0]++;
        if(g_cursorY - i > 0 && g_checkboard[g_cursorX][g_cursorY - i] == 1 && g_checkboard[g_cursorX][g_cursorY - i - 1] != 2 && g_checkboard[g_cursorX][g_cursorY + 1] != 2)//上
            live[1]++;
        if(g_cursorX + i < BOX && g_checkboard[g_cursorX + i][g_cursorY] == 1 && g_checkboard[g_cursorX + i + 1][g_cursorY] != 2 && g_checkboard[g_cursorX - 1][g_cursorY] != 2)//右
            live[2]++;
        if(g_cursorX - i > 0 && g_checkboard[g_cursorX - i][g_cursorY] == 1 && g_checkboard[g_cursorX - i - 1][g_cursorY] != 2 && g_checkboard[g_cursorX + 1][g_cursorY] != 2)//左
            live[3]++;
        if(g_cursorX + i < BOX && g_cursorY + i < BOX && g_checkboard[g_cursorX + i][g_cursorY + i] == 1 && g_checkboard[g_cursorX + i + 1][g_cursorY + i + 1] != 2 && g_checkboard[g_cursorX - 1][g_cursorY -1] != 2)//右下
            live[4]++;
        if(g_cursorX - i > 0 && g_cursorY + i < BOX && g_checkboard[g_cursorX - i][g_cursorY + i] == 1 && g_checkboard[g_cursorX - i - 1][g_cursorY + i +1] != 2 && g_checkboard[g_cursorX - 1][g_cursorY + 1] != 2)//左下
            live[5]++;
        if(g_cursorX + i < BOX && g_cursorY - i > 0 && g_checkboard[g_cursorX + i][g_cursorY - i] == 1 && g_checkboard[g_cursorX + i + 1][g_cursorY - i - 1] != 2 && g_checkboard[g_cursorX + 1][g_cursorY - 1] != 2)//右上
            live[6]++;
        if(g_cursorX - i > 0 && g_cursorY - i > 0 && g_checkboard[g_cursorX - i][g_cursorY - i] == 1 && g_checkboard[g_cursorX - i - 1][g_cursorY - i - 1] != 2 && g_checkboard[g_cursorX + 1][g_cursorY + 1] != 2)//左上
            live[7]++;
    }
    threeNum = 0;
    for(int i = 0;i < 8;i++)
        if(live[i]>=N-1)
            threeNum++;
    if(threeNum >=2)
        return true;
    else
        return false;
     
}